//
//  ProTableViewController.m
//  全国天气解析
//
//  Created by Jonzzs on 16/3/27.
//  Copyright © 2016年 Jonzzs. All rights reserved.
//

#import "ProTableViewController.h"
#import "CityTableViewController.h"

@interface ProTableViewController ()<NSXMLParserDelegate>

@property (nonatomic,strong) NSMutableArray *list;

@end

@implementation ProTableViewController

- (NSMutableArray *)list
{
    if (_list == nil)
    {
        _list = [[NSMutableArray alloc] init];
    }
    return _list;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSURL *url = [NSURL URLWithString:@"http://flash.weather.com.cn/wmaps/xml/china.xml"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSXMLParser *parper = [[NSXMLParser alloc] initWithData:data];
    parper.delegate = self;
    
    [parper parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"city"])
    {
        [self.list addObject:attributeDict];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    NSDictionary *dic = self.list[indexPath.row];
    cell.textLabel.text = dic[@"quName"];
    NSString *str = [NSString stringWithFormat:@"%@  %@°C ~ %@°C  %@",dic[@"stateDetailed"],dic[@"tem1"],dic[@"tem2"],dic[@"windState"]];
    cell.detailTextLabel.text = str;
    
    return cell;
}

#pragma mark - 将要选中表格某行时触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    /* 将选中的省名保存 */
    NSDictionary *dic = self.list[indexPath.row];
    CityTableViewController *city = [[CityTableViewController alloc] init];
    city.name = dic[@"quName"];
    city.pyName = dic[@"pyName"];
    
    [self.navigationController pushViewController:city animated:NO];
}




@end
